//
//  NewsCellBottomView.swift
//  MyTest
//
//  Created by guanzhongwang on 2021/5/9.
//

import UIKit

class NewsCellBottomView: UIView {
    private let nameLabel = UILabel()
    private let descLabel = UILabel()
    private let tipLabel = UILabel()
    private let closebtn = UIButton(type: .custom)
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */
    init() {
        super.init(frame: CGRect.zero)
        addSubview(tipLabel)
        tipLabel.font = Font_Size_12
        tipLabel.textColor = RED_COLOR
        tipLabel.snp.makeConstraints({
            $0.left.equalTo(0)
            $0.centerY.equalToSuperview()
        })
        addSubview(nameLabel)
        nameLabel.font = Font_Size_12
        nameLabel.textColor = B999_TITLE_COLOR
        nameLabel.snp.makeConstraints({
            $0.left.equalTo(tipLabel.snp.right).offset(10)
            $0.top.equalTo(0)
            $0.bottom.equalToSuperview().inset(15)
        })
        
        addSubview(descLabel)
        descLabel.font = Font_Size_12
        descLabel.textColor = B999_TITLE_COLOR
        descLabel.snp.makeConstraints({
            $0.left.equalTo(nameLabel.snp.right).offset(10)
            $0.centerY.equalTo(nameLabel)
        })
        
        closebtn.contentEdgeInsets = UIEdgeInsets.init(top: 5, left: 10, bottom: 5, right: 0)
        var image = UIImage(named: "close")
        image = image?.scaleImage(scaleSize: 0.5)
        image = image?.withRenderingMode(.alwaysTemplate)
        closebtn.tintColor = .lightGray
        
        closebtn.setImage(image, for: .normal)
        addSubview(closebtn)
        closebtn.snp.makeConstraints({
            $0.right.centerY.equalToSuperview()
        })
        closebtn.addTarget(self, action: #selector(deleteCell(btn:)), for: .touchUpInside)
    }
    func updateContent(item:NewsModel) {
        nameLabel.text = item.name
        descLabel.text = "\(item.commentNums) 评论"
        tipLabel.isHidden = !item.isTop && !item.isSubject
        closebtn.isHidden = item.isTop
        if item.isTop {
            tipLabel.text = "置顶"
        }else if item.isSubject{
            tipLabel.text = "专题"
        }
        if tipLabel.isHidden {
            nameLabel.snp.remakeConstraints({
                $0.left.equalTo(0)
                $0.top.equalTo(10)
                $0.bottom.equalToSuperview().inset(10)
            })
        }else{
            nameLabel.snp.remakeConstraints({
                $0.left.equalTo(tipLabel.snp.right).offset(10)
                $0.top.equalTo(10)
                $0.bottom.equalToSuperview().inset(10)
            })
        }
    }
    
    @objc func deleteCell(btn:UIButton){
        let frame = btn.convert(btn.bounds, to: window)
        
        _ = CloseNewsView(frame,x:frame.origin.x)

    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
   
}
